﻿@model Controller2.Models.Saida
@{
    Layout = null;
}
<!DOCTYPE html>
<html class="no-js">
<head>
    <title>Cadastro</title>
    @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.vista.css").Combined(true).Compress(true)))
    @if (!Request.IsAjaxRequest())
    {
        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
        <link href="../../Content/themes/controller/controller.css" rel="stylesheet" type="text/css" />
        @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.vista.css").Combined(true).Compress(true)))  
        <script src="@Url.Content("~/Scripts/jquery.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>    
        <script src="@Url.Content("~/Scripts/jquery.maskedinput-1.3.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    }
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/controller.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/numbers.js")" type="text/javascript"></script>
    <style type="text/css">
        fieldset
        {
            width: 590px;
        }
        .display-label, .editor-label
        {
            width: 130px;
            text-align: right;
            padding-right: 5px;
        }
        textarea#Descricao, #IdMidia
        {
            width: 400px;
        }
        #NomeFornec
        {
            width: 343px;
        }
        #IdFornecedor
        {
            width: 50px;
            text-align: right;
        } 
        #btnFornec
        {
            height: 21px;
            margin: 0;
            padding: 0 5px 20px 5px;
            line-height: 21px;
            vertical-align: top;
        }
    </style>
    @if (false)
    {
        <script src="../../Scripts/jquery-vsdoc.js" type="text/javascript"></script>
        <script src="../../Scripts/jquery-ui.js" type="text/javascript"></script>
        <script src="../../Scripts/controller.js" type="text/javascript"></script>
    }
</head>
<body id="frame">
    <div id="formHide">
        @using (Html.BeginForm("Salvar", "Saida", FormMethod.Post))
        {
            @Html.ValidationSummary(true)
            <fieldset>
                <legend>Saida</legend>
                @Html.HiddenFor(model => model.IdSaida)
                <div class="editor-label">
                    @Html.LabelFor(model => model.DataPrevista, "Data Prevista: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.DataPrevista)
                    @Html.ValidationMessageFor(model => model.DataPrevista)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.DataPagamento, "Data Pagamento: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.DataPagamento)
                    @Html.ValidationMessageFor(model => model.DataPagamento)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.ValorPrevisto, "Valor Previsto: ")
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.ValorPrevisto, new { tipo = "decimal", tamanho = "2", @class = "obrigatorio valor", maxlength = "16" })
                    @Html.ValidationMessageFor(model => model.ValorPrevisto)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.ValorPagamento, "Valor Pagamento: ")
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.ValorPagamento, new { tipo = "decimal", tamanho = "2", @class = "valor", maxlength = "16" })
                    @Html.ValidationMessageFor(model => model.ValorPagamento)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.IdMidia, "Mídia")
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.IdMidia, (IEnumerable<SelectListItem>)ViewBag.Midia, String.Empty)
                    @Html.ValidationMessageFor(model => model.IdMidia)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.IdFornecedor, "Fornecedor: ")
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.IdFornecedor, new { @readonly = "readonly" })
                    @Html.TextBox("NomeFornec", Model.Fornecedor != null ? Model.Fornecedor.NomePessoa : string.Empty, new { @readonly = "readonly" })
                    @Html.ValidationMessageFor(model => model.IdFornecedor)
                    @if (Model.IdSaida <= 0)
                    {
                        <input type="button" id="btnFornec" value="..." />
                    }
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Descricao, "Descrição: ")
                </div>
                <div class="editor-field">
                    @Html.TextAreaFor(model => model.Descricao, new { maxlength = "500", @class = "obrigatorio textArea" })
                    @Html.ValidationMessageFor(model => model.Descricao)
                </div>
            </fieldset>        
            <div id="botoesForm">
                <input type="submit" id="Gravar" value="Gravar" />
                <input type="button" id="Fechar" value="Cancelar" />
            </div>
        }
    </div>
    <div id="alertaErro" style="display: none">
    </div>
    <div id="winFormPesquisaFornecedor" style="display: none">
    </div>
    <script type="text/javascript">
        // custom validate para campos numéricos
        $.validator.methods.qtdeMinima = function (value, element, param) { return $(element).valueFloat() > 0 ? true : false; };
        $.validator.methods.dataValidate = function (value, element, param) { return validaData(element); };
        $(function () {
            $('#DataPrevista').each(function () {
                $(this).rules("add", {
                    dataValidate: true,
                    messages: {
                        dataValidate: "*"
                    }
                });
            });
            $('.valor').each(function () {
                $(this).rules("remove");
                if ($(this).attr('id') == "ValorPrevisto") {
                    $(this).rules("add", {
                        qtdeMinima: true,
                        messages: {
                            qtdeMinima: "*"
                        }
                    });
                }
            });
            if ($('#IdFornecedor').val() == 0) {
                $('#IdFornecedor').rules("remove");
                $('#IdFornecedor').rules('add', { required: true, messages: { required: "*" }});
                $('#IdFornecedor').val('');
            }
            $('.datepicker').mask("99/99/9999");
            textarea_maxlength("Descricao", "500");
            $('#formHide').fadeIn('slow');
            hideLoading();
            $('#ValorPrevisto').focus();
        });
        $('form').submit(function (e) {
            e.preventDefault();
            if ($('form').valid()) {
                $.post('@Url.Action("Salvar", "Saida")', $('form').serialize(), function (data) {
                    if (data.salvo) {
                        closeWindow();
                    } else {
                        alertError(data.error);
                    }
                    $('#gravar').attr('disabled', false);
                });
            }
        });
        $('#Fechar').click(function () {
            closeWindow();
        });
        $('#btnFornec').click(function () {
            showLoading();
            var url = '@Url.Action("PesquisaFornecedor", "Fornecedor")';
            $('#winFormPesquisaFornecedor').empty().load(url);
            formDialog('winFormPesquisaFornecedor', 'Pesquisa de Fornecedores', 460, 700); return false;
        });
        function preencherCamposPesquisaFornecedor(idFornecedor) {
            $.post('@Url.Action("BuscaNomeFornecedor", "Fornecedor")', { id: idFornecedor }, function (data) {
                $('#IdFornecedor').val(idFornecedor);
                $('#NomeFornec').val(data);
            });
            hideLoading();
        }
        function closeWindowPesquisa() {
            $('#winFormPesquisaFornecedor').dialog('close');
            $('#winFormPesquisaFornecedor').removeData();
        }
    </script>
</body>
</html>
